<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools :show-before="true">
        <span slot="before">共{{ page.total }}条记录</span>
        <template slot="after">
          <el-button
            size="small"
            type="primary"
            @click="$router.push('/import')"
            >导入excel</el-button
          >
          <el-button size="small" type="danger">导出excel</el-button>
          <el-button size="small" type="primary" :disabled="checkPermission('POINT-USER-UPDATE')" @click="showDialog = true"
            >新增员工</el-button
          >
          <!-- 放置弹出层，点击弹出 -->
        </template>
      </page-tools>
      <!-- 放置表格和分页 -->
      <el-card v-loading="loading">
        <el-table border :data="list">
          <el-table-column label="序号" sortable="" type="index" />
          <el-table-column label="姓名" sortable="" prop="username" />
          <el-table-column label="工号" sortable="" prop="workNumber" />
          <el-table-column
            label="聘用形式"
            sortable
            :formatter="formatEmployment"
            prop="formOfEmployment"
          />
          <el-table-column label="部门" sortable="" prop="departmentName" />
          <el-table-column label="入职时间" sortable="">
            <template slot-scope="obj">
              {{ obj.row.timeOfEntry | formatDate }}
            </template>
          </el-table-column>
          <el-table-column label="账户状态" sortable="" prop="enableState">
            <template slot-scope="{ row }">
              <el-switch :value="row.enableState === 1" />
            </template>
          </el-table-column>
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template slot-scope="{ row }">
              <el-button
                type="text"
                size="small"
                :disabled="!checkPermission('POINT-USER-UPDATE')"
                @click="$router.push(`/employees/detail/${row.id}`)"
                >查看</el-button
              >
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small" @click="editRole(row.id)"
                >角色</el-button
              >
              <el-button type="text" size="small" @click="delEmployee(row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <el-pagination
            layout="prev, pager, next"
            :page-size="page.size"
            :current-page="page.page"
            :total="page.total"
            @current-change="changePage"
          />
        </el-row>
      </el-card>
    </div>
    <add-demplyee :show-dialog.sync="showDialog" />
    <assign-role
      ref="assignRole"
      :show-role-dialog.sync="showRoleDialog"
      :user-id="userId"
    />
  </div>
</template>

<script>
import { getEmployeList, delEmployee } from "@/api/employees";
import EmployeeEnum from "@/api/constant/employees";
import AddDemplyee from "./components/add-emplyee.vue";
import AssignRole from "./components/assign-role.vue";

export default {
  name: "",
  computed: {},
  components: { AddDemplyee, AssignRole },
  data() {
    return {
      loading: false,
      list: [],
      page: {
        page: 1,
        size: 10,
        total: 0,
      },
      showDialog: false,
      loading: false,
      showRoleDialog: false,
      userId: null,
    };
  },
  created() {
    this.getEmployeList();
    // this.formatEmployment()
  },
  methods: {

    async editRole() {
      // this.userId = id; // props传值 是异步的
      // await this.$refs.AssignRole.getUserDetailById(id); // 父组件调用子组件方法
      this.showRoleDialog = true;
    },

    async delEmployee(id) {
      try {
        await this.$confirm("确定要删除");
        await delEmployee(id);
        await getEmployeList();
      } catch (error) {
        this.$message.error("删除角色失败");
      }
    },
    async getEmployeList() {
      this.loading = true;
      const { total, rows } = await getEmployeList(this.page);
      this.page.total = total;
      this.list = rows;
      this.loading = false;
      // console.log(this.list);
    },
    changePage(newPage) {
      this.page.page = newPage;
      this.getEmployeList();
    },
    formatEmployment(row, column, cellValue, index) {
      const obj = EmployeeEnum.hireType.find((item) => item.id === cellValue);
      // console.log(cellValue);
      // return cellValue;

      return obj ? obj.value : "未知";
    },
  },
};
</script>

<style lang="scss" scoped></style>
